package com.tanhua.dubbo.api.impl.mongo;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.vo.PageResult;
import org.bson.types.ObjectId;

import com.tanhua.domain.db.Question;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.dubbo.api.mongo.FriendApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class FriendApiImpl implements FriendApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    // 保存好友关系到tanhua_users表
    // userId=20  friendId=4
    @Override
    public void save(Long userId, long friendId) {
        // 构造条件
        Query query = new Query(Criteria.where("userId").is(userId)
                .and("friendId").is(friendId)
        );
        // 判断好友是否存在，如果不存在则执行添加
        if (!mongoTemplate.exists(query, Friend.class)) {
            // 创建好友，保存关系
            Friend friend = new Friend();
            friend.setUserId(userId);
            friend.setFriendId(friendId);
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.insert(friend);
        }

        // 继续保存好友关系，双向
        query = new Query(Criteria.where("userId").is(friendId)
                .and("friendId").is(userId)
        );
        if (!mongoTemplate.exists(query, Friend.class)) {
            // 创建好友，保存关系
            Friend friend = new Friend();
            friend.setUserId(friendId);
            friend.setFriendId(userId);
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.insert(friend);
        }

    }

    // 查询好友列表
    //db.tanhua_users.find({userId:20})
    @Override
    public PageResult findByPage(Long userId, Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.limit(pagesize).skip((page-1)*pagesize);
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        long count = mongoTemplate.count(query, Friend.class);
        return new PageResult(page,pagesize, (int) count,friendList);
    }

    /**
     * 判断是否为好友
     */
    @Override
    public Boolean isFriend(Long userId, Long friendId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
                        .and("friendId").is(friendId)
        );
        return mongoTemplate.exists(query, Friend.class);
    }

    /**
     * 删除双向好友关系
     *
     * @param userId     now user
     * @param likeUserId friendId
     */
    @Override
    public void delete(Long userId, Long likeUserId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
                        .and("friendId").is(likeUserId)
        );
        mongoTemplate.remove(query, Friend.class);
        query = new Query(
                Criteria.where("userId").is(likeUserId)
                        .and("friendId").is(userId)
        );
        mongoTemplate.remove(query, Friend.class);
    }
}
